import utilfuncs from "./tools/utilfuncs";
var funcs = utilfuncs.UtilFuncs;

import globalConst from "./globalConst";
var global = globalConst.GlobalConst;

var PathUnitType = {}
PathUnitType.line = "line";
PathUnitType.arc = "arc";
PathUnitType.bezier = "bezier";
PathUnitType.quadratic = "quadratic";
PathUnitType.circle = "circle";
PathUnitType.polygon = "polygon";
PathUnitType.spline = "spline";
PathUnitType.ellipseArc = "ellipseArc";
PathUnitType.ellipse = "ellipse";

class TTempGraphUnit {
    constructor(refGraph) {
        this.type = "unkown";
        this.pos = null;
        this.pt1 = null;
        this.pt2 = null;
        this.breakFlag = false;//已被打断为最小单元
        this.findInterGraphFlag = false;//搜索过与其相交的图形
        this.refGraph = refGraph;//所属的图形对象
        this.independFlag = false;//由独立的封闭图形生成，目前暂时只有独立的，无相交的圆形此标志为true
    }
    same(dstUnit) {
        if (dstUnit == this) {
            return false;
        }
        if (dstUnit.type != this.type) {
            return false;
        }

        let flag = false;
        switch (this.type) {
            case PathUnitType.line:
                flag = this.compareLineUnit(dstUnit);
                break;

            case PathUnitType.arc:
                flag = this.compareArcUnit(dstUnit);
                break;

            case PathUnitType.circle:
                flag = this.compareCircleUnit(dstUnit);
                break;

            case PathUnitType.polygon:
                flag = this.comparePolygonUnit(dstUnit);
                break;
        }
        return flag;
    }

    compareLineUnit(dstUnit) {
        let p1 = this.pos.posList[0];
        let p2 = this.pos.posList[1];

        let dstp1 = dstUnit.pos.posList[0];
        let dstp2 = dstUnit.pos.posList[1];
        let dis1 = funcs.distancePoint(p1, dstp1, 1);
        let dis2 = funcs.distancePoint(p2, dstp2, 1);
        if (dis1 < global.ZERO_4E && dis2 < global.ZERO_4E) {
            return true;
        }

        let dis3 = funcs.distancePoint(p2, dstp1, 1);
        let dis4 = funcs.distancePoint(p1, dstp2, 1);
        if (dis3 < global.ZERO_4E && dis4 < global.ZERO_4E) {
            return true;
        }
        return false;
    }


    compareArcUnit(dstUnit) {
        let cnt1 = this.pos.getCnt();
        let cnt2 = dstUnit.pos.getCnt();
        let dis = funcs.distancePoint(cnt1, cnt2, 1);
        if (dis > global.ZERO_4E) {
            return false;
        }

        if (this.pos.antiClockWise != dstUnit.pos.antiClockWise) {
            return false;
        }

        dis = Math.abs(this.pos.radius - dstUnit.pos.radius);
        if (dis > global.ZERO_4E) {
            return false;
        }

        let angleDeta1 = Math.abs(this.pos.stAngle - dstUnit.pos.stAngle);
        let angleDeta2 = Math.abs(this.pos.endAngle - dstUnit.pos.endAngle);
        if (angleDeta1 < global.ZERO_4E && angleDeta2 < global.ZERO_4E) {
            return true;
        }
        return false;
    }

    compareCircleUnit(dstUnit) {
        let cnt1 = this.pos.getCnt();
        let cnt2 = dstUnit.pos.getCnt();
        let dis = funcs.distancePoint(cnt1, cnt2, 1);
        if (dis > global.ZERO_4E) {
            return false;
        }

        dis = Math.abs(this.pos.radius - dstUnit.pos.radius);
        if (dis > global.ZERO_4E) {
            return false;
        }
        return true;
    }



    comparePolygonUnit(dstUnit) {
        return false;
    }

}

class TEdgeInfo {
    constructor() {
        this.type = "unkown";
        this.codeDesc = null;
    }
}

class TNode {
    constructor(pt) {
        this.id = 0;
        this.pt = { x: pt.x, y: pt.y };
        this.connPathUnitList = []; //关联的TPathUnit队列
        this.setFlag = false;
    }

    addConnUnit(topoPath) {
        if (this.connPathUnitList.indexOf(topoPath) == -1) {
            this.connPathUnitList.push(topoPath);
        }
    }

    chkConnUnitToltalyOverlap(){
        let overLapList = [];
        let list = this.connPathUnitList;
        for (let i = 0;i < list.length;i++){
            let srcUnit = list[i];
            let srcRefGraphUnit = srcUnit.refGraphUnit;
            for (let n = 0;n < list.length;n++){
                let dstUnit = list[n];
                let dstRefGraphUnit = dstUnit.refGraphUnit;
                if (n != i){
                    if (srcRefGraphUnit.same(dstRefGraphUnit)){
                        if (overLapList.indexOf(srcRefGraphUnit) == -1) {
                            overLapList.push(srcRefGraphUnit);
                        }
                    }
                }
            }
        }
        return overLapList;
    }
}

class TPathUnit {
    constructor(unit) {
        this.id = 0;
        this.stNode = null;
        this.endNode = null;
        this.refGraphUnit = unit;
        this.setFlag = false;
    }
    setNode(node) {
        if (!this.stNode) {
            this.stNode = node;
        }
        else if (!this.endNode) {
            this.endNode = node;
        }
        node.addConnUnit(this);
    }
}

//用于求边界相交图形
class TInterGrahInfo {
    constructor(graph) {
        this.graph = graph;//对应AbsGraph对象
        this.point = { x: 0, y: 0 };
        this.dis = 0;
    }
}


//边界相交图形初步获取后，用于求相交图形被打断后的单元求交
class TInterUnitInfo {
    constructor(pathUnit) {
        this.pathUnit = pathUnit;//对应TPathUnit对象
        this.point = { x: 0, y: 0 };
        this.dis = 0;
    }
}



export default { TPathUnit, TNode, TEdgeInfo, TTempGraphUnit, PathUnitType, TInterGrahInfo, TInterUnitInfo }